package com.jeecms.wicool.manager.user.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.user.UserCouponDao;
import com.jeecms.wicool.entity.shops.Coupon;
import com.jeecms.wicool.entity.user.UserCoupon;
import com.jeecms.wicool.entity.user.UserInfo;
import com.jeecms.wicool.manager.shops.CouponMng;
import com.jeecms.wicool.manager.user.UserCouponMng;
import com.jeecms.wicool.manager.user.UserInfoMng;

@Service
@Transactional
public class UserCouponMngImpl implements UserCouponMng {

	private UserCouponDao dao;

	@Autowired
	public void setDao(UserCouponDao dao) {
		this.dao = dao;
	}

	@Transactional(readOnly = true)
	public Pagination getPage(Integer userId, Integer couponType,
			String mobile,String couponName, Integer couponSource, Short status, int pageNo,
			int pageSize, CmsUser user) {
		Pagination page = dao.getPage(userId, couponType,mobile, couponName,
				couponSource, status, pageNo, pageSize, user);
		return page;
	}

	@Override
	public UserCoupon findById(Integer id) {
		UserCoupon entity = dao.findById(id);
		return entity;
	}

	@Override
	public UserCoupon update(UserCoupon bean, Integer userId,Integer couponId) {
		UserInfo user = userMng.findById(userId);
		Coupon coupon = couponMng.findById(couponId);
		bean.setUser(user);
		bean.setCoupon(coupon);
		Updater<UserCoupon> updater = new Updater<UserCoupon>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}

	@Override
	public UserCoupon save(UserCoupon bean, Integer userId,Integer couponId) {

		UserInfo user = userMng.findById(userId);
		Coupon coupon = couponMng.findById(couponId);
		bean.setUser(user);
		bean.setCoupon(coupon);
		dao.save(bean);
		return bean;
	}

	@Override
	public UserCoupon deleteById(Integer id) {
		UserCoupon bean = dao.deleteById(id);
		return bean;
	}

	@Override
	public UserCoupon[] deleteByIds(Integer[] ids) {
		UserCoupon[] beans = new UserCoupon[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	@Autowired
	private UserInfoMng userMng;
	@Autowired
	private CouponMng couponMng;

}